﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data ;
using System.Windows.Forms ;

namespace FlowCard
{
    class LogHelper
    {
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="strOperator">操作者</param>
        /// <param name="strGDNo">工单号</param>
        /// <param name="dr">数据</param>
        public static void WriteLog(string strOperator,string strGDNo, DataRow dr)
        {
            SQLTools sqt = new SQLTools();
            string strInserLog = "insert into CardPrintLog (GDNo,Operator,PrintTime,Data) values ('@GDNo','@Operator',GETDATE (),'@Data')";
            string strTmpData = string.Empty;
            for (int i = 0; i < dr.ItemArray.Length; i++)
            {
                if (dr.Table.Columns[i].ColumnName.Equals("条形码"))
                {
                    continue;
                }
                strTmpData += GetString(dr[i]);
                if (i < dr.ItemArray.Length - 1)
                {
                    strTmpData += "|";
                }
            }
            sqt.DoCmdExecuteNonQuery(strInserLog.Replace("@GDNo", strGDNo)
                                                                                  .Replace("@Operator", strOperator)
                                                                                  .Replace("@Data", strTmpData));
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="strOperator">操作者</param>
        /// <param name="strGDNo">工单号</param>
        /// <param name="dr">数据</param>
        public static void WriteLog(string strOperator, string strGDNo,DataGridViewRow dgvr )
        {
            SQLTools sqt = new SQLTools();
            string strInserLog = "insert into CardPrintLog (GDNo,Operator,PrintTime,Data) values ('@GDNo','@Operator',GETDATE (),'@Data')";
            string strTmpData = string.Empty;
            for (int i = 0; i <dgvr.Cells .Count ; i++)
            {
                if (dgvr.DataGridView .Columns[i].Name .Equals("条形码"))
                {
                    continue;
                }
                strTmpData += GetString(dgvr .Cells [i].Value );
                if (i < dgvr.Cells.Count - 1)
                {
                    strTmpData += "|";
                }
            }
            sqt.DoCmdExecuteNonQuery(strInserLog.Replace("@GDNo", strGDNo)
                                                                                  .Replace("@Operator", strOperator)
                                                                                  .Replace("@Data", strTmpData));
        }

        /// <summary>
        /// 使指定工单的打印记录失效
        /// </summary>
        /// <param name="strOperator">操作者</param>
        /// <param name="strID">日志编号</param>
        public static void MakeLogFailure(string strOperator,string strID)
        {
            SQLTools sqt = new SQLTools();
            string strUpdateLog = "update CardPrintLog set Failure=1,Editor='@Operator',EditTime=GETDATE () where ID=@ID";
            sqt.DoCmdExecuteNonQuery(strUpdateLog.Replace("@Operator", strOperator)
                                                                                        .Replace("@ID", strID));
        }

        private static string GetString(object tmpobj)
        {
            try
            {
                return tmpobj.ToString();
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}
